#!/usr/bin/env python

# content_management_dialog - Content management GUI for Entertainer.
# Copyright (C) 2007 Lauri Taimila
# + following additions Copyright 2008 Joshua Scotton <josh@joshuascotton.com>
#       * added on_button_open_list_clicked
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import sys
import os
import string
import ConfigParser
from ConfigParser import ParsingError

from backend.core.message import Message
from backend.core.message_bus_proxy import MessageBusProxy
from backend.core.message_type_priority import MessageType
from utils.open_feed_source_dialog import OpenFeedSourceDialog

try:
 	import pygtk
  	pygtk.require("2.4")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)
	
class ContentManagementDialog:
    """This is a content management tool for Entertainer media center application."""
    
    # Temporary storage for entered URL
    url = ""
    
    # Is this dialog running as a stand alone process
    __STAND_ALONE = None

    def __init__(self, stand_alone):
        # Open and parse configuration file
        self.__STAND_ALONE = stand_alone
        self.config = ConfigParser.ConfigParser()
        try:
            self.config.readfp(open(os.path.expanduser('~/.entertainer/content.conf')))
        except ParsingError:
            print "Syntax error in configuration file: " + os.path.expanduser('~/.entertainer/content.conf')
            print "Execution aborted!"
            sys.exit(1)
        except IOError:
            print "Configuration file is missing: " + os.path.expanduser('~/.entertainer/content.conf')
            print "Execution aborted!"
            #FIXME: We should create a default setting file if file is missing
            sys.exit(1)
        except:
            print "Unknown error occured when configuration file was loaded!"
            print "Execution aborted!"
            sys.exit(1)
            
        # Load glade UI
        self.gladefile = "utils/glade/entertainer-content-management.glade"
        self.widgets = gtk.glade.XML(self.gladefile)
        
        # Get content management dialog and bind signal callbacks
        self.dialog = self.widgets.get_widget("ContentManagementDialog")
        if (self.dialog):
            callback_dic = { "on_button_open_list_clicked" : self.on_button_open_list_clicked,
                             "on_close_button_clicked" : self.on_close_button_clicked,
                             "on_button_remove_videos_clicked" : self.on_button_remove_videos_clicked,
                             "on_button_add_videos_clicked" : self.on_button_add_videos_clicked,
                             "on_button_edit_videos_clicked" : self.on_button_edit_videos_clicked,
                             "on_checkbutton_video_metadata_toggled" : self.on_checkbutton_video_metadata_toggled,
                             "on_button_add_music_clicked" : self.on_button_add_music_clicked,
                             "on_button_remove_music_clicked" : self.on_button_remove_music_clicked,
                             "on_button_edit_music_clicked" : self.on_button_edit_music_clicked,
                             "on_lyrics_checkbox_toggled" : self.on_lyrics_checkbox_toggled,
                             "on_art_checkbox_toggled" : self.on_art_checkbox_toggled,
                             "on_button_add_images_clicked" : self.on_button_add_images_clicked,
                             "on_button_remove_images_clicked" : self.on_button_remove_images_clicked,
                             "on_button_edit_images_clicked" : self.on_button_edit_images_clicked,
                             "on_button_add_feed_clicked" : self.on_button_add_feed_clicked,
                             "on_button_remove_feed_clicked" : self.on_button_remove_feed_clicked,
                             "on_button_edit_feed_clicked" : self.on_button_edit_feed_clicked,
                             "on_fetch_interval_spinbutton_value_changed" : self.on_fetch_interval_spinbutton_value_changed,
	                         "on_ContentManagementDialog_destroy" : self.on_dialog_closed,
	                         "on_url_dialog_delete_event" : self.on_url_dialog_delete_event,
	                         "on_url_dialog_ok_button_clicked" : self.on_url_dialog_ok_button_clicked,
	                         "on_url_dialog_cancel_button_clicked" : self.on_url_dialog_cancel_button_clicked,
	                         "on_button_video_rebuild_clicked" : self.on_button_video_rebuild_clicked,
	                         "on_button_music_rebuild_clicked" : self.on_button_music_rebuild_clicked,
	                         "on_button_image_rebuild_clicked" : self.on_button_image_rebuild_clicked,
	                         "on_button_feed_rebuild_clicked" : self.on_button_feed_rebuild_clicked }
        self.widgets.signal_autoconnect(callback_dic)
        
        # Initilize dialog widgets with correct values and show dialog
        self.init_dialog_values_from_configure_file()
        self.dialog.resize(500,300)
        self.dialog.show()

# Signal handlers 

    def on_dialog_closed(self, widget):
        """Callback function for dialog's close button"""
        try:
            proxy = MessageBusProxy(client_name = "Content Management GUI")
            proxy.connectToMessageBus()
            proxy.sendMessage(Message(MessageType.CONTENT_CFG_UPDATED))
            proxy.disconnectFromMessageBus()
        except:
            pass # This means that backend is not running. Doesn't matter
        
        if(self.__STAND_ALONE):
            self.dialog.hide()
            self.dialog.destroy()
            gtk.main_quit()
        else:
            self.dialog.hide()
            self.dialog.destroy()
    
    def on_close_button_clicked(self, widget):
        """Callback function for dialog's close button"""
        try:
            proxy = MessageBusProxy(client_name = "Content Management GUI")
            proxy.connectToMessageBus()
            proxy.sendMessage(Message(MessageType.CONTENT_CFG_UPDATED))
            proxy.disconnectFromMessageBus()
        except:
            pass # This means that backend is not running. Doesn't matter
        
        if(self.__STAND_ALONE):
            self.dialog.hide()
            self.dialog.destroy()
            gtk.main_quit()
        else:
            self.dialog.hide()
            self.dialog.destroy()
        
    def on_button_add_videos_clicked(self, widget):
        """Opens add URL dialog. """
        widget = self.widgets.get_widget("treeview_videos")
        model = widget.get_model()
        # Open "Select folder" dialog
        dialog =  gtk.FileChooserDialog("Select video folder", None, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK), None)
        status = dialog.run()	
        # If folder was selected we add it to model and update config file
        if(status == gtk.RESPONSE_OK):
            selected_folder = dialog.get_current_folder()
            model.append([selected_folder])
	    if(self.video_folders == None):
			self.video_folders = [selected_folder]
	    else:
            		self.video_folders.append(selected_folder)
            str_folders = string.join(self.video_folders, ';')
            self.config.set("Videos","folders", str_folders)
            cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
            self.config.write(cfg_file)
        dialog.destroy()
        
    def on_button_remove_videos_clicked(self, widget):
        """Remove currently selected folder from video folders"""
        widget = self.widgets.get_widget("treeview_videos")
        model = widget.get_model()
        selection = widget.get_selection().get_selected()
        try:
            rm_folder = model.get_value(selection[1], 0)
            self.video_folders.remove(rm_folder)
            str_folders = string.join(self.video_folders, ';')
            self.config.set("Videos","folders", str_folders)
            cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
            self.config.write(cfg_file)
            model.remove(selection[1])
        except:
            pass
    
    def on_button_edit_videos_clicked(self, widget):
        """Edit currently selected folder"""
        widget = self.widgets.get_widget("treeview_videos")
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        model = widget.get_model()
        try:
            selection = widget.get_selection().get_selected()
            folder = model.get_value(selection[1], 0)
            url_entry.set_text(folder)
            url_dialog.set_title("Edit URL")
            status = url_dialog.run()
            if status == gtk.RESPONSE_OK and os.path.exists(self.url):
                # Update list model
                model.set_value(selection[1], 0, self.url)
                # Update configure file
                pos = self.video_folders.index(folder)
                self.video_folders.remove(folder)
                self.video_folders.insert(pos, self.url)
                str_folders = string.join(self.video_folders, ';')
                self.config.set("Videos","folders", str_folders)
                cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
                self.config.write(cfg_file)
        except:
            # No need for actions. Item wasn't selected.
            pass
    
    def on_checkbutton_video_metadata_toggled(self, widget):
        """Download video file metadata from internet"""
        self.config.set("Videos","download_metadata", widget.get_active())
        cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'),'w')
        self.config.write(cfg_file)
    
    def on_button_add_music_clicked(self, widget):
        """Opens add URL dialog. """
        widget = self.widgets.get_widget("treeview_music")
        model = widget.get_model()
        # Open "Select folder" dialog
        dialog =  gtk.FileChooserDialog("Select music folder", None, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK), None)
        status = dialog.run()
        # If folder was selected we add it to model and update config file
        if(status == gtk.RESPONSE_OK):
            selected_folder = dialog.get_current_folder()
            model.append([selected_folder])
            if(self.music_folders == None):
		self.music_folders = [selected_folder]
	    else:
            	self.music_folders.append(selected_folder)
            str_folders = string.join(self.music_folders, ';')
            self.config.set("Music","folders", str_folders)
            cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
            self.config.write(cfg_file)
        dialog.destroy()
        
    def on_button_remove_music_clicked(self, widget):
        """Remove currently selected folder from music folders"""
        widget = self.widgets.get_widget("treeview_music")
        model = widget.get_model()
        selection = widget.get_selection().get_selected()
        try:
            rm_folder = model.get_value(selection[1], 0)
            self.music_folders.remove(rm_folder)
            str_folders = string.join(self.music_folders, ';')
            self.config.set("Music","folders", str_folders)
            cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'),'w')
            self.config.write(cfg_file)
            model.remove(selection[1])
        except:
            # No need for actions. Item wasn't selected.
            pass
     
    def on_button_edit_music_clicked(self, widget):
        """Edit currently selected music folder"""
        widget = self.widgets.get_widget("treeview_music")
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        model = widget.get_model()
        try:
            selection = widget.get_selection().get_selected()
            folder = model.get_value(selection[1], 0)
            url_entry.set_text(folder)
            url_dialog.set_title("Edit URL")
            status = url_dialog.run()
            if status == gtk.RESPONSE_OK and os.path.exists(self.url):
                # Update list model
                model.set_value(selection[1], 0, self.url)
                # Update configure file
                pos = self.music_folders.index(folder)
                self.music_folders.remove(folder)
                self.music_folders.insert(pos, self.url)
                str_folders = string.join(self.music_folders, ';')
                self.config.set("Music","folders", str_folders)
                cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
                self.config.write(cfg_file)
        except:
            # No need for actions. Item wasn't selected.
            pass
    
    def on_button_add_images_clicked(self, widget):
        """Opens add URL dialog. """
        widget = self.widgets.get_widget("treeview_images")
        model = widget.get_model()
        # Open "Select folder" dialog
        dialog =  gtk.FileChooserDialog("Select image folder", None, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, (gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK), None)
        status = dialog.run()
        # If folder was selected we add it to model and update config file
        if(status == gtk.RESPONSE_OK):
            selected_folder = dialog.get_current_folder()
            model.append([selected_folder])
	    if(self.image_folders == None):
		self.image_folders = [selected_folder]
            else:
            	self.image_folders.append(selected_folder)
            str_folders = string.join(self.image_folders, ';')
            self.config.set("Images","folders", str_folders)
            cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
            self.config.write(cfg_file)
        dialog.destroy()
        
    def on_button_remove_images_clicked(self, widget):
        """Remove currently selected folder from images folders"""
        widget = self.widgets.get_widget("treeview_images")
        model = widget.get_model()
        selection = widget.get_selection().get_selected()
        try:
            rm_folder = model.get_value(selection[1], 0)
            self.image_folders.remove(rm_folder)
            str_folders = string.join(self.image_folders, ';')
            self.config.set("Images","folders", str_folders)
            cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'),'w')
            self.config.write(cfg_file)
            model.remove(selection[1])
        except:
            # No need for actions. Item wasn't selected.
            pass
    
    def on_button_edit_images_clicked(self, widget):
        """Edit currently selected music folder"""
        widget = self.widgets.get_widget("treeview_images")
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        model = widget.get_model()
        try:
            selection = widget.get_selection().get_selected()
            folder = model.get_value(selection[1], 0)
            url_entry.set_text(folder)
            url_dialog.set_title("Edit URL")
            status = url_dialog.run()
            if status == gtk.RESPONSE_OK and os.path.exists(self.url):
                # Update list model
                model.set_value(selection[1], 0, self.url)
                # Update configure file
                pos = self.image_folders.index(folder)
                self.image_folders.remove(folder)
                self.image_folders.insert(pos, self.url)
                str_folders = string.join(self.image_folders, ';')
                self.config.set("Images","folders", str_folders)
                cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
                self.config.write(cfg_file)
        except:
            # No need for actions. Item wasn't selected.
            pass
    
    def on_button_add_feed_clicked(self, widget):
        """Opens add feed dialog. """
        widget = self.widgets.get_widget("treeview_feeds")
        url_dialog = self.widgets.get_widget("url_dialog")
        model = widget.get_model()
        # Open dialog
        url_dialog.set_title("Add RSS-feed")
        status = url_dialog.run()
        # If folder was selected we add it to model and update config file
        if(status == gtk.RESPONSE_OK):
            model.append([self.url])
            self.feeds.append(self.url)
            str_folders = string.join(self.feeds, ';')
            self.config.set("RSS","feeds", str_folders)
            cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
            self.config.write(cfg_file)
            
    def on_button_remove_feed_clicked(self, widget):
        """Remove currently selected reed from RSS-feeds"""
        widget = self.widgets.get_widget("treeview_feeds")
        model = widget.get_model()
        selection = widget.get_selection().get_selected()
        try:
            rm_folder = model.get_value(selection[1], 0)
            self.feeds.remove(rm_folder)
            str_folders = string.join(self.feeds, ';')
            self.config.set("RSS","feeds", str_folders)
            cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'),'w')
            self.config.write(cfg_file)
            model.remove(selection[1])
        except:
            # No need for actions. Item wasn't selected.
            pass
        
    def on_button_edit_feed_clicked(self, widget):
        """Edit currently selected feed"""
        widget = self.widgets.get_widget("treeview_feeds")
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        model = widget.get_model()
        try:
            selection = widget.get_selection().get_selected()
            feed = model.get_value(selection[1], 0)
            url_entry.set_text(feed)
            url_dialog.set_title("Edit feed")
            status = url_dialog.run()
            if status == gtk.RESPONSE_OK:
                # Update list model
                model.set_value(selection[1], 0, self.url)
                # Update configure file
                pos = self.feeds.index(feed)
                self.feeds.remove(feed)
                self.feeds.insert(pos, self.url)
                str_feeds = string.join(self.feeds, ';')
                self.config.set("RSS","feeds", str_feeds)
                cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'), 'w')
                self.config.write(cfg_file)
        except:
            # No need for actions. Item wasn't selected.
            pass
    
    def on_button_open_list_clicked(self, widget):
        """Opens the open feed source dialog"""
        OpenFeedSourceDialog(self.widgets.get_widget("treeview_feeds"),self.feeds,self.config)
    
    def on_fetch_interval_spinbutton_value_changed(self, widget):
        self.config.set("RSS","fetch_interval", widget.get_value_as_int())
        cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'),'w')
        self.config.write(cfg_file)

    def on_lyrics_checkbox_toggled(self, widget):
        self.config.set("Music","download_lyrics", widget.get_active())
        cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'),'w')
        self.config.write(cfg_file)
    
    def on_art_checkbox_toggled(self, widget):
        self.config.set("Music","download_album_art", widget.get_active())
        cfg_file = file(os.path.expanduser('~/.entertainer/content.conf'),'w')
        self.config.write(cfg_file)
        
    def on_url_dialog_ok_button_clicked(self, widget):
        """URL dialog OK button pressed. Sets self.url"""
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        url_dialog.hide()
        self.url = url_entry.get_text()
        url_entry.set_text("")
        url_dialog.response(gtk.RESPONSE_OK)
        
    def on_url_dialog_cancel_button_clicked(self, widget):
        """URL dialog cancelled. Hides dialog"""
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        url_dialog.hide()
        url_entry.set_text("")
        url_dialog.response(gtk.RESPONSE_CANCEL)
        
    def on_url_dialog_delete_event(self, widget, data):
        """Dialog's X clicked. Hides dialog"""
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        url_dialog.hide()
        url_entry.set_text("")
        url_dialog.response(gtk.RESPONSE_CANCEL)
        return True
        
    def on_button_video_rebuild_clicked(self, widget):
        """Rebuild video cache requested"""
        try:
            proxy = MessageBusProxy(client_name = "Content Management GUI")
            proxy.connectToMessageBus()
            proxy.sendMessage(Message(MessageType.REBUILD_VIDEO_CACHE))
            proxy.disconnectFromMessageBus()
        except:
		    # This means that backend is not running. Doesn't matter
		    error = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Entertainer backend is not running. Cache cannot be rebuilt.")
		    error.run()
		    error.destroy()
            
    def on_button_music_rebuild_clicked(self, widget):
        """Rebuild music cache requested"""
        try:
            proxy = MessageBusProxy(client_name = "Content Management GUI")
            proxy.connectToMessageBus()
            proxy.sendMessage(Message(MessageType.REBUILD_MUSIC_CACHE))
            proxy.disconnectFromMessageBus()
        except:
		    # This means that backend is not running. Doesn't matter
		    error = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Entertainer backend is not running. Cache cannot be rebuilt.")
		    error.run()
		    error.destroy()
            
    def on_button_image_rebuild_clicked(self, widget):
        """Rebuild image cache requested"""
        try:
            proxy = MessageBusProxy(client_name = "Content Management GUI")
            proxy.connectToMessageBus()
            proxy.sendMessage(Message(MessageType.REBUILD_IMAGE_CACHE))
            proxy.disconnectFromMessageBus()
        except:
		    # This means that backend is not running. Doesn't matter
		    error = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Entertainer backend is not running. Cache cannot be rebuilt.")
		    error.run()
		    error.destroy()
            
    def on_button_feed_rebuild_clicked(self, widget):
        """Rebuild feed cache requested"""
        #We need the user to confirm the rebuild feed cache request
        dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING, gtk.BUTTONS_OK_CANCEL, "This will completely remove any feed entries in the cache!")
        status = dialog.run()
        #If user has ok'd the request send the message to the message bus
        if(status == gtk.RESPONSE_OK):
			try:
				proxy = MessageBusProxy(client_name = "Content Management GUI")
				proxy.connectToMessageBus()
				proxy.sendMessage(Message(MessageType.REBUILD_FEED_CACHE))
				proxy.disconnectFromMessageBus()
			except:
			    # This means that backend is not running. Doesn't matter
			    error = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Entertainer backend is not running. Cache cannot be rebuilt.")
			    error.run()
			    error.destroy()
        dialog.destroy()

    
# Signal handlers end       
        
    def init_dialog_values_from_configure_file(self):
        """Read configure file and set dialog widget values with read values."""
        ########################################################################
        # Videos
        ########################################################################
	videolist_widget = self.widgets.get_widget("treeview_videos")
	videostore = gtk.ListStore(str)
        folder_list = self.config.get("Videos", "folders") # Error threat

	cell_renderer = gtk.CellRendererText()
	column = gtk.TreeViewColumn("Folders", cell_renderer, text=0)
	videolist_widget.append_column(column)

	# If there is no configuration, we don't populate with an empty result
	if(folder_list.isspace() == True or folder_list == ""):
		self.video_folders = None
	else:		
		print("folder List : " +folder_list)
		self.video_folders = folder_list.split(';')

		# Fill model with folders read from config file
		for i in range(len(self.video_folders)):
		    videostore.insert(i, [self.video_folders[i]])

	videolist_widget.set_model(videostore)
        
        # Checkboxes
        metadata_checkbox = self.widgets.get_widget("video_metadata_checkbox")
        if self.config.has_section("Videos"):
            md_val = self.config.getboolean("Videos", "download_metadata") # Error threat
            metadata_checkbox.set_active(md_val)
        
        ########################################################################
        # Music
        ########################################################################
        music_list = self.config.get("Music", "folders") # Error threat
	musiclist_widget = self.widgets.get_widget("treeview_music")
        music_model = gtk.ListStore(str)

	music_cell = gtk.CellRendererText()
	music_column = gtk.TreeViewColumn("Images", music_cell, text=0)
	musiclist_widget.append_column(music_column)

	# If there is no configuration, we don't populate with an empty result
	if(music_list.isspace() == True or music_list == ""):
		self.music_folders = None
	else:	
		self.music_folders = music_list.split(';')

		# Fill model with folders read from config file
		for i in range(len(self.music_folders)):
		    music_model.insert(i, [self.music_folders[i]])

	musiclist_widget.set_model(music_model)
        
        # Checkboxes
        art_checkbox = self.widgets.get_widget("art_checkbox")
        lyrics_checkbox = self.widgets.get_widget("lyrics_checkbox")
        if self.config.has_section("Music"):
            art_val = self.config.getboolean("Music", "download_album_art") # Error threat
            lyrics_val = self.config.getboolean("Music", "download_lyrics") # Error threat
            art_checkbox.set_active(art_val)
            lyrics_checkbox.set_active(lyrics_val)
        
        ########################################################################
        # Images
        ########################################################################
        image_list = self.config.get("Images", "folders") # Error threat
        imagelist_widget = self.widgets.get_widget("treeview_images")
        images_model = gtk.ListStore(str)

	img_cell = gtk.CellRendererText()
	img_column = gtk.TreeViewColumn("Images", img_cell, text=0)
	imagelist_widget.append_column(img_column)

	# If there is no configuration, we don't populate with an empty result
	if(image_list.isspace() == True or image_list == ""):
		self.image_folders = None
	else:
		self.image_folders = image_list.split(';')

		# Fill model with folders read from config file
		for i in range(len(self.image_folders)):
		    images_model.insert(i, [self.image_folders[i]])

	imagelist_widget.set_model(images_model)
        
        ########################################################################
        # RSS-feeds
        ########################################################################
        feed_list = self.config.get("RSS", "feeds") # Error threat
        self.feeds = feed_list.split(';')
        
        feedlist_widget = self.widgets.get_widget("treeview_feeds")
        feed_model = gtk.ListStore(str)
        
        rss_cell = gtk.CellRendererText()
        rss_column = gtk.TreeViewColumn("Feeds", rss_cell, text=0)
        feedlist_widget.append_column(rss_column)

        # Fill model with folders read from config file
        for i in range(len(self.feeds)):
            feed_model.insert(i, [self.feeds[i]])

        feedlist_widget.set_model(feed_model)
        
        # Interval spinner
        interval_spinner = self.widgets.get_widget("fetch_interval_spinbutton")
        if self.config.has_section("RSS"):
            interval_val = self.config.getint("RSS", "fetch_interval") # Error threat
            if interval_val < 15:
                interval_val = 15
            elif interval_val > 900:
                interval_val = 900
            interval_spinner.set_value(interval_val)
